﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CtrLayer;
using ModelLayer;

namespace GUILayer
{
    public partial class GUIPlanningShow : Form
    {
        MovieController movieCtr = new MovieController();
        List<ComboBox> comboBoxes = new List<ComboBox>();
        Dictionary<int, Movie> schedule = new Dictionary<int, Movie>();
        public GUIPlanningShow()
        {
            InitializeComponent();
            comboBoxes.Add(comboBoxPlanning1300);
            comboBoxes.Add(comboBoxPlanning1330);
            comboBoxes.Add(comboBoxPlanning1400);
            comboBoxes.Add(comboBoxPlanning1430);
            comboBoxes.Add(comboBoxPlanning1500);
            comboBoxes.Add(comboBoxPlanning1530);
            comboBoxes.Add(comboBoxPlanning1600);
            comboBoxes.Add(comboBoxPlanning1630);
            comboBoxes.Add(comboBoxPlanning1700);
            comboBoxes.Add(comboBoxPlanning1730);
            comboBoxes.Add(comboBoxPlanning1800);
            comboBoxes.Add(comboBoxPlanning1830);
            comboBoxes.Add(comboBoxPlanning1900);
            comboBoxes.Add(comboBoxPlanning1930);
            comboBoxes.Add(comboBoxPlanning2000);
            comboBoxes.Add(comboBoxPlanning2030);
            comboBoxes.Add(comboBoxPlanning2100);
            comboBoxes.Add(comboBoxPlanning2130);
            comboBoxes.Add(comboBoxPlanning2200);
            comboBoxes.Add(comboBoxPlanning2230);
        }

        private void buttonPlanningBack_Click(object sender, EventArgs e)
        {
            Form newForm = new GUIStart();
            this.Hide();
            newForm.ShowDialog();
            this.Close();
        }

        private void monthCalendarPlanning_DateChanged(object sender, DateRangeEventArgs e)
        {
            populateComboboxes();
        }

        private void populateComboboxes()
        {
            List<Movie> movies = movieCtr.GetMovies(monthCalendarPlanning.SelectionStart);

            //Clear schedule for newly selected day.
            schedule.Clear();
            foreach (ComboBox comboBox in comboBoxes)
            {
                comboBox.Enabled = true;
                comboBox.Items.Clear();
                comboBox.Items.AddRange(movies.ToArray());
            }
        }

        private void enableComboboxes(bool enable, int startSlot, int playLength)
        {
            //Enable/Disabled all reserved slots.
            int endSlot = calcEndSlot(startSlot, playLength);
            for (int i = (startSlot + 1); i < endSlot; i++)
            {
                comboBoxes[i].Enabled = enable;
            }
        }

        private bool checkSchedule(int startSlot, int playLength)
        {
            //Assume schedule is free.
            bool returnState = false;
            //Creates loop to check all necessary positions in the schedule.
            for (int i = startSlot; i <= calcEndSlot(startSlot, playLength); i++)
            {
                //Check each schedule key, and change returnState if anything is found.
                if (schedule.ContainsKey(i))
                {
                    returnState = true;
                }
            }
            //Return result.
            return returnState;
        }

        private int calcEndSlot(int startSlot, int playLength)
        {
            //Calculate required amount of slots for the given playlength and add startSlot.
            int endSlot = (startSlot + (int)Math.Ceiling((double)playLength / 30));
            //Calculate endslot position.
            if (endSlot > comboBoxes.Count)
            {
                endSlot = comboBoxes.Count;
            }
            return endSlot;
        }

        private void selectMovie(int startSlot, Movie movie)
        {
            //Create reset movie object.
            Movie nullMovie = new Movie("Nulstil");

            //Check previous selection: When previous selection is found.
            if (schedule.ContainsKey(startSlot))
            {
                //Release reserved slots.
                enableComboboxes(true, startSlot, schedule[startSlot].PlayLength);
                //Remove previous selection from schedule.
                schedule.Remove(startSlot);
            }

            //Check current selection: When selection is reset movie object.
            if (movie.Title == nullMovie.Title)
            {
                //Remove reset movie object.
                comboBoxes[startSlot].Items.RemoveAt(0);
                //Deselect current selection.
                //Remove previous selection from schedule.
                schedule.Remove(startSlot);
            }
            //Check current selection: When selection is not reset movie object.
            else
            {
                //Check if required slots are free.
                if (checkSchedule(startSlot, movie.PlayLength) == true)
                {
                    //Deselect item.............................................................................
                }
                else
                {
                    //Reserve Slots for selected movie.
                    enableComboboxes(false, startSlot, movie.PlayLength);
                    //Add movie to schedule.
                    schedule.Add(startSlot, movie);
                    //Add reset item for deselection.
                    comboBoxes[startSlot].Items.Insert(0, nullMovie);
                }
            }
        }
        /*
        private void selectMovie(int startSlot, Movie movie)
        {
            //Create reset movie object.
            Movie nullMovie = new Movie("Nulstil");
            //Check schedule position and previous selected movie playlength.
            if (schedule.ContainsKey(startSlot) && (schedule[startSlot].PlayLength > 0))
            {
                //Reenable reserved slots and remove previous selected movie from schedule.
                enableComboboxes(true, startSlot, schedule[startSlot].PlayLength);
                schedule.Remove(startSlot);
            }

            //Check if reset movie object has been selected.
            if (nullMovie.Title == movie.Title)
            {
                //Remove reset movie object from combobox.
                comboBoxes[startSlot].Items.RemoveAt(0);
                comboBoxes[startSlot].SelectedIndex = -1;
            }
            else
            {
                //Check if required slots are free.
                if (checkSchedule(startSlot, movie.PlayLength) == true)
                {
                    comboBoxes[startSlot].SelectedIndex = -1;
                }
                else
                {
                    //Disable slots and add movie to schedule.
                    enableComboboxes(false, startSlot, movie.PlayLength);
                    schedule.Add(startSlot, movie);
                    //Add reset movie object to beginning of combobox.
                    comboBoxes[startSlot].Items.Insert(0, nullMovie);
                }
            }
        }
        */
        private void comboBoxPlanning1300_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectMovie(0, (Movie)comboBoxPlanning1300.SelectedItem);
        }

        private void comboBoxPlanning1330_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectMovie(1, (Movie)comboBoxPlanning1330.SelectedItem);
        }

        private void comboBoxPlanning1400_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectMovie(2, (Movie)comboBoxPlanning1400.SelectedItem);
        }

        private void comboBoxPlanning1430_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectMovie(3, (Movie)comboBoxPlanning1430.SelectedItem);
        }

        private void comboBoxPlanning1500_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectMovie(4, (Movie)comboBoxPlanning1500.SelectedItem);
        }

        private void comboBoxPlanning1530_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectMovie(5, (Movie)comboBoxPlanning1530.SelectedItem);
        }

        private void comboBoxPlanning1600_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectMovie(6, (Movie)comboBoxPlanning1600.SelectedItem);
        }

        private void comboBoxPlanning1630_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectMovie(7, (Movie)comboBoxPlanning1630.SelectedItem);
        }

        private void comboBoxPlanning1700_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectMovie(8, (Movie)comboBoxPlanning1700.SelectedItem);
        }

        private void comboBoxPlanning1730_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectMovie(9, (Movie)comboBoxPlanning1730.SelectedItem);
        }

        private void comboBoxPlanning1800_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectMovie(10, (Movie)comboBoxPlanning1800.SelectedItem);
        }

        private void comboBoxPlanning1830_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectMovie(11, (Movie)comboBoxPlanning1830.SelectedItem);
        }

        private void comboBoxPlanning1900_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectMovie(12, (Movie)comboBoxPlanning1900.SelectedItem);
        }

        private void comboBoxPlanning1930_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectMovie(13, (Movie)comboBoxPlanning1930.SelectedItem);
        }

        private void comboBoxPlanning2000_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectMovie(14, (Movie)comboBoxPlanning2000.SelectedItem);
        }

        private void comboBoxPlanning2030_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectMovie(15, (Movie)comboBoxPlanning2030.SelectedItem);
        }

        private void comboBoxPlanning2100_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectMovie(16, (Movie)comboBoxPlanning2100.SelectedItem);
        }

        private void comboBoxPlanning2130_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectMovie(17, (Movie)comboBoxPlanning2130.SelectedItem);
        }

        private void comboBoxPlanning2200_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectMovie(18, (Movie)comboBoxPlanning2200.SelectedItem);
        }

        private void comboBoxPlanning2230_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectMovie(19, (Movie)comboBoxPlanning2230.SelectedItem);
        }

        private void GUIPlanningShow_Load(object sender, EventArgs e)
        {
            populateComboboxes();
        }
    }
}
